package es.udc.emarketapp.emarket.model.shippingFacade;

import es.udc.emarketapp.emarket.model.shipping.vo.ShippingVO;
import es.udc.emarketapp.emarket.model.shippingFacade.actions.CalculateShippingCostsAction;
import es.udc.emarketapp.emarket.model.shippingFacade.actions.CountriesRatesBySellerAction;
import es.udc.emarketapp.emarket.model.shippingFacade.actions.CreateShippingAction;
import es.udc.emarketapp.emarket.model.shippingFacade.actions.CountryRatesAction;
import es.udc.emarketapp.emarket.model.shippingFacade.actions.DeleteShippingCountryAction;
import es.udc.emarketapp.emarket.model.shippingFacade.actions.ModifyShippingsCountryAction;
import es.udc.emarketapp.emarket.model.shippingFacade.vo.ShippingsCountryVO;
import es.udc.emarketapp.emarket.model.shippingFacade.vo.ShippingsSellerChunkVO;
import es.udc.fbellas.j2ee.util.exceptions.InstanceNotFoundException;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.exceptions.ModelException;
import es.udc.fbellas.j2ee.util.sql.PlainActionProcessor;
import es.udc.fbellas.j2ee.util.sql.SimpleDataSource;
import javax.sql.DataSource;

public class ShippingFacade {

    private DataSource dataSource;

    public ShippingFacade() throws InternalErrorException {

        dataSource = new SimpleDataSource();

    }

    public ShippingVO createShipping(ShippingVO shippingVO)
            throws InternalErrorException {

        try {
            CreateShippingAction action = new CreateShippingAction(shippingVO);
            return (ShippingVO) PlainActionProcessor.process(
                    dataSource, action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
    } // createShipping

    public Double calculateShippingCosts(String idSeller,
            String country, Double weight) throws InternalErrorException {

        try {

            CalculateShippingCostsAction action =
                    new CalculateShippingCostsAction(idSeller, country, weight);

            return (Double) PlainActionProcessor.process(dataSource, action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }
    } // calculateShippingCosts

    public ShippingsCountryVO findCountryRates(String idSeller, 
            String idCountry, String language)
            throws InternalErrorException {

        try {
                    CountryRatesAction action =
                    new CountryRatesAction(idSeller, idCountry, language);

            return (ShippingsCountryVO) PlainActionProcessor.process(dataSource, action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }
    } // findCountryRates
    
    
        public ShippingsSellerChunkVO findCountriesRatesBySeller(String idSeller, String language,
                int startIndex, int count)
            throws InternalErrorException {

        try {

            CountriesRatesBySellerAction action =
                    new CountriesRatesBySellerAction(idSeller, language, startIndex, count);

            return (ShippingsSellerChunkVO) PlainActionProcessor.process(dataSource, action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }
    } // findCountriesRatesBySeller



     public ShippingsCountryVO modifyShippingsCountry(ShippingsCountryVO shippingCountry)
            throws InternalErrorException {

        try {

            ModifyShippingsCountryAction action =
                    new ModifyShippingsCountryAction(shippingCountry);

            return (ShippingsCountryVO) PlainActionProcessor.process(dataSource, action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }
    } // modifyShippingsCountry


         public void deleteShippingCountry(String idSeller, String idCountry)
            throws InternalErrorException, InstanceNotFoundException {

        try {
 System.out.println("deleteShippingCountry idSeller: " + idSeller + "idCountry: " + idCountry);

 DeleteShippingCountryAction action =
                    new DeleteShippingCountryAction(idSeller, idCountry);

            PlainActionProcessor.process(dataSource, action);

        } catch (InternalErrorException e) {
            throw e;
        } catch (ModelException e) {
            throw new InternalErrorException(e);
        }
    } // deleteShippingCountry

    
} // class

